package skiree.host.abetsy.api.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import skiree.host.abetsy.api.annotation.EventQueueServer;
import skiree.host.abetsy.api.annotation.ThreadPoolUtil;
import skiree.host.abetsy.api.dao.ConfigDao;
import skiree.host.abetsy.api.service.ConfigService;
import skiree.host.abetsy.api.vessel.Result;
import skiree.host.abetsy.model.LocalConfig;

import javax.annotation.Resource;
import java.util.*;

@Component
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private EventQueueServer eventQueueServer;

    @Resource
    private ConfigDao configDao;

    @Override
    public Result settingInfo() {
        List<LocalConfig> localConfigs = configDao.selectList(null);
        if (localConfigs.isEmpty()) {
            return Result.error(501, "获取全局配置失败!");
        }
        Map<String, Object> info = new HashMap<>();
        for (LocalConfig config : localConfigs) {
            if (config.getName().equals("priority")) {
                info.put(config.getName(), JSONUtil.parseArray(config.getValue()));
            }
            if (StrUtil.equalsAny(config.getName(), "expand", "episodeSort")) {
                info.put(config.getName(), Boolean.parseBoolean(config.getValue()));
            }
            if (StrUtil.equalsAny(config.getName(), "coreSize", "maxSize")) {
                info.put(config.getName(), Integer.parseInt(config.getValue()));
            }
        }
        return Result.success(info);
    }

    @Override
    public Result syncPriority(String priorityStr) {
        LocalConfig localConfig = new LocalConfig();
        localConfig.setName("priority");
        localConfig.setValue(priorityStr);
        configDao.insertOrUpdate(localConfig);
        return Result.success(localConfig);
    }

    @Override
    public Result syncExpand(String expandStr) {
        LocalConfig localConfig = new LocalConfig();
        localConfig.setName("expand");
        localConfig.setValue(expandStr);
        configDao.insertOrUpdate(localConfig);
        return Result.success(localConfig);
    }

    @Override
    public Result syncEpisodeSort(String episodeSortStr) {
        LocalConfig localConfig = new LocalConfig();
        localConfig.setName("episodeSort");
        localConfig.setValue(episodeSortStr);
        configDao.insertOrUpdate(localConfig);
        return Result.success(localConfig);
    }

    @Override
    public int getCoreSize() {
        QueryWrapper<LocalConfig> configQueryWrapper = new QueryWrapper<>();
        configQueryWrapper.eq("name", "coreSize");
        LocalConfig localConfigs = configDao.selectOne(configQueryWrapper);
        return localConfigs == null ? 2 : Integer.parseInt(localConfigs.getValue());
    }

    @Override
    public int getMaxSize() {
        QueryWrapper<LocalConfig> configQueryWrapper = new QueryWrapper<>();
        configQueryWrapper.eq("name", "maxSize");
        LocalConfig localConfigs = configDao.selectOne(configQueryWrapper);
        return localConfigs == null ? 4 : Integer.parseInt(localConfigs.getValue());
    }

    @Override
    public Result syncThreadPool(String threadPoolStr) {
        JSONObject jsonObject = JSONUtil.parseObj(threadPoolStr);
        int coreSize = jsonObject.getInt("coreSize", 2);
        int maxSize = jsonObject.getInt("maxSize", 4);
        LocalConfig localConfig = new LocalConfig();
        localConfig.setName("coreSize");
        localConfig.setValue(String.valueOf(coreSize));
        configDao.insertOrUpdate(localConfig);
        localConfig.setName("maxSize");
        localConfig.setValue(String.valueOf(maxSize));
        configDao.insertOrUpdate(localConfig);
        eventQueueServer.SettingExecutor(coreSize, maxSize);
        return Result.success(jsonObject);
    }

    @Override
    public Result getEventQueue() {
        Map<String, Object> info = ThreadPoolUtil.getInstance().getPoolInfo();
        return Result.success(info);
    }

}
